//
//  ViewController.m
//  15-UICollectionViewLifeCycle
//
//  Created by dllo on 16/8/3.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "MyCollectionViewCell.h"
#define kCollectionViewReuse @"reuse"
#define COLORAUTO [UIColor colorWithRed:(arc4random() % 256 / 255.0) green:(arc4random() % 256 / 255.0) blue:(arc4random() % 256 / 255.0) alpha:1.0]

@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((self.view.bounds.size.width - 5) / 2, 100);
    flowLayout.minimumLineSpacing = 5;
    flowLayout.minimumInteritemSpacing = 5;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    collectionView.backgroundColor = [UIColor whiteColor];
    [collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewReuse];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    collectionView.refreshControl = [[UIRefreshControl alloc] init];
}
// 关于CollectionView LifeCycle
// 1 numberOfSectionsInCollectionView
// 2 collectionView:numberOfItemsInSection:
// 注意 3-5 循环执行
// 3 prepareForReuse(从重用池取出时调用)
// 4 collectionView:cellForItemAtIndexPath:
// 5 collectionView:willDisplayCell:
// 当cell完全出屏幕时
// 6 collectionView:didEndDisplayingCell:
// iOS 10 以前
// 出屏幕立即返回, 依旧进入3-4-5
// iOS 10 以后
// 出屏幕立即返回执行4-5 不执行3




#pragma mark - collectionView LifeCycle
// 区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    NSLog(@"%@--%s", self, __FUNCTION__);
    return 1;
}
// 区里的cell个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSLog(@"%@--%s", self, __FUNCTION__);
    return 100;
}
// 从重用池取出cell
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewReuse forIndexPath:indexPath];
    cell.backgroundColor = COLORAUTO;
    return cell;
}
// iOS 8.0之后推出 cell将要显示在屏幕上时
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@---%ld----[%@: %s]", cell, indexPath.row, self, __FUNCTION__);
}
// iOS 8.0之后推出 cell已经离开屏幕时
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@---%ld----[%@: %s]", cell, indexPath.row, self, __FUNCTION__);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
